<template>
  <el-dialog
    append-to-body
    :visible.sync="visible"
    :before-close="beforeClose"
    width="800"
    title="数据管理"
    :close-on-click-modal="false"
  >
    <table class="table table-bordered text-center table-vertical-middle">
      <thead>
        <tr>
          <th>任务模块</th>
          <th width="120px">操作</th>
        </tr>
      </thead>
      <tr v-for="(p, index) of tableData" :key="index">
        <td>{{ p.name }}</td>
        <td>
          <el-button type="primary" @click="uploadData(p.type)" size="mini">导入</el-button>
        </td>
      </tr>
    </table>

    <import-excel
      :visible.sync="icVisible"
      title="导入工商数据"
      :post-api="importUrl"
      excel-template="/static/files/工商信息导入模板.xlsx"
    ></import-excel>

    <import-excel
      :visible.sync="familyVisible"
      title="家庭成员导入"
      :post-api="importUrl"
      excel-template="/static/files/家庭成员导入模板.xlsx"
    ></import-excel>

    <import-excel
      :visible.sync="insuranceVisible"
      title="导入保险"
      :post-api="importUrl"
      excel-template="/static/files/保险导入模板.xlsx"
    ></import-excel>

    <import-excel
      :visible.sync="subsidyVisible"
      title="导入补贴"
      :post-api="importUrl"
      excel-template="/static/files/补贴导入模板.xlsx"
    ></import-excel>

    <import-excel
      :visible.sync="powerVisible"
      title="导入用电信息"
      :post-api="importUrl"
      excel-template="/static/files/用电信息导入模板.xlsx"
    ></import-excel>

    <import-excel
      :visible.sync="machineryVisible"
      title="导入农机信息"
      :post-api="importUrl"
      excel-template="/static/files/农机信息导入模板.xlsx"
    ></import-excel>

    <el-dialog
      append-to-body
      :visible.sync="customerVisible"
      :before-close="customerClose"
      width="800"
      title="经营主体"
      :close-on-click-modal="false"
    >
      <table class="table table-bordered text-center table-vertical-middle">
        <thead>
          <tr>
            <th>任务模块</th>
            <th width="120px">操作</th>
          </tr>
          <tr>
            <td>500亩大坝主体</td>
            <td>
              <el-button type="primary" @click="DamUpload" size="mini">导入</el-button>
            </td>
          </tr>
          <tr>
            <td>适度规模主体</td>
            <td>
              <el-button type="primary" @click="routineUpload" size="mini">导入</el-button>
            </td>
          </tr>
          <tr>
            <td>产业包主体</td>
            <td>
              <el-button type="primary" @click="goodsUpload" size="mini">导入</el-button>
            </td>
          </tr>
        </thead>
      </table>
    </el-dialog>
    <import-excel
      :visible.sync="damVisible"
      title="导入主体信息"
      :post-api="importUrl"
      excel-template="/static/files/主体信息导入模板(大坝).xlsx"
    ></import-excel>
    <import-excel
      :visible.sync="routineVisible"
      title="导入主体信息"
      :post-api="importUrl"
      excel-template="/static/files/主体信息导入模板(常规).xlsx"
    ></import-excel>
    <import-excel
      :visible.sync="goodsVisible"
      title="导入主体信息"
      :post-api="importUrl"
      excel-template="/static/files/主体信息导入模板(产业包).xlsx"
    ></import-excel>
  </el-dialog>
</template>

<script>
import { queryTrouble, queryUrl } from '@vimi/utils-api'

import ImportExcel from '@/components/ImportExcel'

export default {
  components: {
    ImportExcel,
  },
  props: {
    visible: Boolean,
  },
  data() {
    return {
      tableData: [
        { name: '经营主体', type: 1 },
        { name: '工商数据', type: 2 },
        { name: '家庭成员', type: 3 },
        { name: '保险信息', type: 4 },
        { name: '补贴数据', type: 5 },
        { name: '用电数据', type: 6 },
        { name: '农机数据', type: 7 },
      ],

      customerVisible: false,

      icVisible: false,
      icTableName: 'datum_customer_info_warehouse',

      familyVisible: false,

      insuranceVisible: false,
      insuranceTableName: 'datum_customer_insurance_order_import',

      subsidyVisible: false,
      subsidyTableName: 'datum_customer_finance_capital_source_import',

      powerVisible: false,
      powerTableName: 'datum_customer_ele_table_import',

      machineryVisible: false,
      machineryTableName: 'datum_customer_business_produce_import',

      customerTableName: 'customer_related',

      damVisible: false,
      routineVisible: false,
      goodsVisible: false,
      importUrl: '',
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {},
  methods: {
    beforeClose() {
      this.$emit('update:visible', false)
    },
    DamUpload() {
      this.importUrl =
        '/api/credit/import/' +
        queryTrouble(this.customerTableName, {
          importKey: `${this.customerTableName}_template`,
          'service_id.eq': this.$store.getters.org.clientId,
          'submit_type.eq': 1,
          'project_type.eq': 2,
        })
      this.damVisible = true
    },
    routineUpload() {
      this.importUrl =
        '/api/credit/import/' +
        queryTrouble(this.customerTableName, {
          importKey: `${this.customerTableName}_regular_template`,
          'service_id.eq': this.$store.getters.org.clientId,
          'submit_type.eq': 1,
          'project_type.eq': 1,
        })
      this.routineVisible = true
    },
    goodsUpload() {
      this.importUrl =
        '/api/credit/import/' +
        queryTrouble(this.customerTableName, {
          importKey: `${this.customerTableName}_template`,
          'service_id.eq': this.$store.getters.org.clientId,
          'submit_type.eq': 1,
          'project_type.eq': 3,
        })
      this.goodsVisible = true
    },
    uploadData(type) {
      switch (type) {
        case 1:
          this.customerUpload()
          break
        case 2:
          this.icUpload()
          break
        case 3:
          this.familyUpload()
          break
        case 4:
          this.insuranceUpload()
          break
        case 5:
          this.subsidyUpload()
          break
        case 6:
          this.powerUpload()
          break
        case 7:
          this.machineryUpload()
          break
        default:
          break
      }
    },
    customerUpload() {
      this.customerVisible = true
    },
    customerClose() {
      this.customerVisible = false
    },
    icUpload() {
      this.importUrl =
        '/api/credit/import/' +
        queryTrouble(this.icTableName, {
          importKey: `${this.icTableName}_template`,
        })
      this.icVisible = true
    },
    familyUpload() {
      this.importUrl = queryUrl({
        service: this.$serviceName,
        table: 'import/',
        params: {
          importKey: 'datum_customer_family_emp_warehouse_template',
        },
      })
      this.familyVisible = true
    },
    insuranceUpload() {
      this.importUrl = `/api/credit/import/?importKey=${this.insuranceTableName}_template`
      this.insuranceVisible = true
    },
    subsidyUpload() {
      this.importUrl = `/api/credit/import/?importKey=${this.subsidyTableName}_template`
      this.subsidyVisible = true
    },
    powerUpload() {
      this.importUrl = `/api/credit/import/?importKey=${this.powerTableName}_template`
      this.powerVisible = true
    },
    machineryUpload() {
      this.importUrl = `/api/credit/import/?importKey=${this.machineryTableName}_template`
      this.machineryVisible = true
    },
  },
}
</script>
<style lang="scss" scoped>
.table-vertical-middle td {
  vertical-align: middle;
}
</style>
